﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Shop.Controllers
{
    public class OneItemController :BaseController
    {
        // GET: Shop/OneItem
        public ActionResult Index(string code, string state, string openid)
        {
            AppSetting setting = new AppSetting();
            List<ProjectModel> list = new List<ProjectModel>();
            try
            {


                if (string.IsNullOrEmpty(openid))
                {
                    openid = GetOpenId(code).openid;

                    if (string.IsNullOrEmpty(openid))
                    {
                        Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxed78d8fdb0030e29&redirect_uri=http%3a%2f%2f{0}%2fshop%2fmain&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect", setting.AppDomainName.Replace("http://", "")));
                    }
                }

                using (var db = new qianjingEntities())
            {
                var records = db.D_item.OrderByDescending(a => a.DIMID).Where(a => (a.RowStatus == 1) && a.dimType == 2).ToList();
                foreach (var item in records)
                {
                    list.Add(new ProjectModel
                    {
                        dimDesc = item.dimDesc,
                        dimName = item.dimName,
                        dimTotalRate=Convert.ToInt16(item.dimTotalRate),
                        DIMID=Convert.ToInt16(item.DIMID),
                    });
                }
            }
            ViewBag.OpenId = openid;

            }
            catch (Exception ex)
            {
                Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx719afdb0aa0ebec4&redirect_uri=http%3a%2f%2fwww.qianjing-china.com%2fthousandclear%2fshop%2fOneItem&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
            }
            return View(list);
        }
        // POST: Shop/OneItem
        [HttpPost]
        public ActionResult Index(string data)
        {
            RouteValueDictionary dicgt = new RouteValueDictionary();
            try
            {
                AppSetting setting = new AppSetting();
                var post = JObject.Parse(data);

                string price = post["price"].Value<string>();
                UtilNS.Util.WriteLog(System.DateTime.Now + "price::" + price);
                string OpenId = post["OpenId"].Value<string>();
                UtilNS.Util.WriteLog(System.DateTime.Now + "OpenId::" + OpenId);
                string stringSelectModel = post["stringSelectModel"].Value<string>();
                UtilNS.Util.WriteLog(System.DateTime.Now + "toItemsGift::" + stringSelectModel);
                string Number = DateTime.Now.ToString("yyMMddHHmmss");
                Random example = new Random();
                int Numradom = example.Next(10000, 99999);
                Number = Number + Numradom.ToString();
                UtilNS.Util.WriteLog(System.DateTime.Now + "toSerialNo::" + Number + "toItemsGift::" + stringSelectModel);
                using (var db = new qianjingEntities())
                {
                    var toOCID = db.O_Customer.FirstOrDefault(a => a.ocOpenid == OpenId).OCID;

                    db.T_Order.Add(new T_Order
                    {
                        toSerialNo = Number,
                        toGoodNum = 1,
                        toOCID = Convert.ToInt16(toOCID),
                        toTotalRate = Convert.ToDecimal(price),
                        toTime = 1,
                        RowStatus = 4,
                        CreatedDate = DateTime.Now,
                        toItemsGift = stringSelectModel,
                        toOGID = 100,
                    });
                    db.SaveChanges();
                }

                dicgt.Add("openid", OpenId);
                dicgt.Add("order", Number);
            }
            catch (Exception ex)
            {

            }
            
            return RedirectToAction("../Address/Index", dicgt);
        }
    }
}